import Vue from 'vue'
import Vuex from 'vuex'
import $mConfig from '@/config/index.config.js'
import $mRoutesConfig from '@/config/routes.config.js'
import $mRouter from '@/common/router.js'

Vue.use(Vuex)

const TOKEN = uni.getStorageSync("token") || "";
const SRC = uni.getStorageSync("src") || "";
const OPENID = uni.getStorageSync("openId") || "";
const USER_INFO = uni.getStorageSync("userInfo") || {};
const CUSTOMER_INFO = uni.getStorageSync("customerInfo") || {};
// 点击去另一个权限的页面,没有登录的时候显示弹出框
const showBeforeLogin = uni.getStorageSync("showBeforeLogin") || false;
const showBeforeValidate = uni.getStorageSync("showBeforeValidate") || false;
console.log(USER_INFO)
const store = new Vuex.Store({
	state: {
		// 是否強制登录
		forcedLogin:$mConfig.forcedLogin,
		// 前端token
		token: TOKEN,
		// 用户openid
		openId: OPENID,
		// 用户信息 头像 昵称
		userInfo: USER_INFO,
		// 用户的商户信息
		customerInfo: CUSTOMER_INFO,
		//下拉更多
		isMore: false, 
		toTheEnd: false,
		isEmpty: false,
		// 未登录前使用modal
		showBeforeLogin:showBeforeLogin,
		// 未实名认证前使用modal
		showBeforeValidate:showBeforeValidate,
		// 合同管理查看签署
		src:SRC,
		//项目id
		projectId:'',
		// 注册邀请码
		inviteCode:'',
		//我的邀请码
		myInviteCode:''
	},
	getters: {
		// 用户是否登录
		hasLogin: state => {
			if (state.token) {
				return true;
			} else {
				return false
			}
		},
		// 用户是否实名认证
		hasCheck: state => {
			if(state.userInfo){
				if (state.userInfo.is_check === 1) {
					return true;
				} else {
					return false
				}
			} else {
				return false
			}
		},
		// 是否设置了认证了手机
		hasSetPayPhone: state => {
			if(state.userInfo){
				if (state.userInfo.safeMobile === 1) {
					return true;
				} else {
					return false
				}
			} else {
				return false
			}
		}, 
		// 是否设置了支付密码
		hasSetPayPsw: state => {
			if(state.userInfo){
				if (state.userInfo.pay_password === 1) {
					return true;
				} else {
					return false
				}
			} else {
				return false
			}
			
		}, 
		// 是否签订了银行协议
		hasSign: state => {
			if(state.userInfo){
				if (state.userInfo.pay_password === 2) {
					return true;
				} else {
					return false
				}
			} else {
				return false
			}
			
		}, 
	},
	mutations: {
		SetShowLoginModal(state, showBeforeLogin) {
			state.showBeforeLogin = showBeforeLogin;
			uni.setStorageSync("showBeforeLogin", showBeforeLogin);
		},
		SetShowValidateModal(state, showBeforeValidate) {
			state.showBeforeValidate = showBeforeValidate;
			uni.setStorageSync("showBeforeValidate", showBeforeValidate);
		},
		SetInviteCode(state, inviteCode) {
			state.inviteCode = inviteCode;
		},
		SET_TOKEN(state, token) {
			state.token = token;
			uni.setStorageSync("token", token);
		},
		SET_OPENID(state, openId) {
			state.openId = openId;
			uni.setStorageSync("openId", openId);
		},
		SET_USERINFO(state, userInfo) {
			state.userInfo = userInfo;
			uni.setStorageSync("userInfo", userInfo);
		},
		SET_CUSTOMERINFO(state, customerInfo) {
			state.customerInfo = customerInfo;
			uni.setStorageSync("customerInfo", customerInfo);
		},
		SET_LOGOUT(state) {
			state.token = "";
			uni.setStorageSync("token", "");
		},
		SET_SRC(state, info) {
			state.src = info;
			uni.setStorageSync("src", info);
		},
		changeIsMore(state, info){
		  state.isMore = info;
		},
		changeIsEnd(state, info){
		  state.toTheEnd = info;
		},
		changeIsEmpty(state, info){
		  state.isEmpty = info;
		},
		changeProjectId(state,projectId){
		  state.projectId=projectId
		}
	},
	actions: {
		// 登录过期 重新登录
		reLogin({
			commit
		}, info) {
			commit("SET_TOKEN","");
			$mRouter.redirectTo({
				route:$mRoutesConfig.login
			});
		}
	}
})

export default store
